package com.yixun.qfbao.service.impl;

/**
 * @author zilong.liu
 * @date 2021-01-04
 * @desc 
 * 		员工部门二维码记录表
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.dao.PartnerDeptQrcodeDao;
import com.yixun.qfbao.mapper.PartnerInfoMapper;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.service.GetWeAppQrCodeService;
import com.yixun.qfbao.xf.model.PartnerDeptQrCode;

/**
 * 
 * @author zilong.liu
 * @date 2020-12-31
 *
 */
@Service("partnerDeptQrcodeService")
public class PartnerDeptQrcodeServiceImpl implements GetWeAppQrCodeService {

	
	@Autowired
	PartnerInfoMapper partnerInfoMapper;
	
	@Autowired
	PartnerDeptQrcodeDao partnerDeptQrcodeDao;

	
	private ThreadLocal<PartnerInfo> partnerLocal = new ThreadLocal<>();
	
	/**
	 * 获取当前人员部门对应的二维码链接
	 * @param userId
	 * @param id
	 * @param strategy
	 * @return
	 */
	@Override
	public String getWeAppQrCodeUrl(String userId, Long id, String... strategy) {
		//1、 判断ID是否为空，如果为空，则查询部门ID赋值给当前ID
		if(id == null || id == 0L) {
			PartnerInfo partnerInfo = partnerInfoMapper.selectById(Long.parseLong(userId));
			this.partnerLocal.set(partnerInfo);
			id = partnerInfo.getDeptId();
		}
		//2、根据部门ID以及人员ID从表中获取
		PartnerDeptQrCode partnerDeptQrcode = new PartnerDeptQrCode();
		partnerDeptQrcode.setPartnerId(Long.parseLong(userId));
		partnerDeptQrcode.setDeptId(id);
		PartnerDeptQrCode partnerDeptQrCode = partnerDeptQrcodeDao.selectByPartnerIdAndDeptId(partnerDeptQrcode);
		//3、返回
		return partnerDeptQrCode != null? partnerDeptQrCode.getQrCodeUrl(): null;
	}

	/**
	 * 获取小程序二维码中携带的参数值
	 * @param userId
	 * @param id
	 * @param companyUuid
	 * @param strategy
	 * @return
	 */
	@Override
	public String getWeAppScene(String userId, Long id, String companyUuid, String... strategy) {
		if(id == null || id == 0L) {
			PartnerInfo partnerInfo = this.partnerLocal.get();
			id = partnerInfo.getDeptId();
		}
		return userId + SCENE_SPLIT_CHARACTER + id;
	}

	/**
	 * 保存二维码
	 * @param userId
	 * @param id
	 * @param qrCodeUrl
	 * @param strategy
	 * @return
	 */
	@Override
	public int saveQrCodeUrl(String userId, Long id, String qrCodeUrl, String... strategy) {
		if(id == null || id == 0L) {
			PartnerInfo partnerInfo = this.partnerLocal.get();
			id = partnerInfo.getDeptId();
			partnerLocal.remove();
		}
		PartnerDeptQrCode partnerDeptQrCode = new PartnerDeptQrCode();
		partnerDeptQrCode.setCreateUserId(Long.parseLong(userId));
		partnerDeptQrCode.setUpdateUserId(Long.parseLong(userId));
		partnerDeptQrCode.setDeptId(id);
		partnerDeptQrCode.setPartnerId(Long.parseLong(userId));
		partnerDeptQrCode.setQrCodeUrl(qrCodeUrl);
		return partnerDeptQrcodeDao.add(partnerDeptQrCode);
	}

}